/**
 * 
 */
package com.kingcore.director.calendar.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.dao.impl.DirectorDaoJdbcImpl;
import com.kingcore.director.calendar.dao.CalendarDao;
import com.kingcore.director.reminder.bean.AwokeBean;
import com.kingcore.framework.context.ApplicationContext;

/**
 * @author Zeven
 *
 */
public class CalendarDaoImpl extends DirectorDaoJdbcImpl implements
		CalendarDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public static void main(String[] args) {
		System.out.println(8%3);
		System.out.println(-8%3);
	}


	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	public List<AwokeBean> getScheduleList(String dateStr) throws Exception {
		String sql = "SELECT ID, SortId, TITLE, BEGINTIME,NEXTTIME,ENDTIME,TIMETYPE" +
				", INTERVAL,INTERVALNUM, USEFUL, NEEDAFFIRM, CONTENT,AwokeStyle" +
				" FROM "+DirectorConstants.RMD_Awoke +
				" Where (Useful='Y' or INTERVALNUM=0) "+   //提醒有效的 or当时有效的
				"   And BEGINTIME<=format('"+dateStr+" 23:59:59','yyyy-mm-dd HH:mm:ss') " +  //这天之前开始的
				//" Order by SortId asc, Id asc" +
				" Order by Nexttime asc";
		List<AwokeBean> retList = new ArrayList<AwokeBean>();

		Connection conn= null;
		Statement stmt =null;
		ResultSet rs=null;
		try {
			String id = null;
			String title = null;
			String nextTime = null;
			Integer intervalNum = null;
			String interval = null;
			String beginTime = null;
			Date selDate = sdf.parse(dateStr+" 00:00:00");
			Date dt_beginTime = null;
			Calendar sel_cale = Calendar.getInstance();
			Calendar beg_cale = Calendar.getInstance();
			sel_cale.setTime(selDate);
			
			Long timeInterval = null;
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				id = rs.getString("ID");
				beginTime = rs.getString("BEGINTIME"); 
				nextTime = rs.getString("NEXTTIME");
				title = rs.getString("TITLE");
				intervalNum = rs.getInt("INTERVALNUM");
				if(StringUtils.isBlank(nextTime)){
					nextTime = beginTime;
				}
				dt_beginTime = sdf.parse(beginTime);
				beg_cale.setTime(dt_beginTime);
				
				if (dateStr.equals( nextTime.subSequence(0, 10))) { //完全匹配在同一天
					AwokeBean bean = new AwokeBean();
					bean.setId( id );
					bean.setTitle( title );
					bean.setNextTime( nextTime );
					retList.add(bean);
				} else if(intervalNum>0) {
					interval = rs.getString("INTERVAL");
					
					//对月、年特殊处理
					if ("year".equals(interval)) {
						if(sel_cale.get(Calendar.DAY_OF_MONTH)!=beg_cale.get(Calendar.DAY_OF_MONTH)
								|| sel_cale.get(Calendar.MONTH)!=beg_cale.get(Calendar.MONTH) ){
							continue;  //天和月不同，本次取消
						}
						int yearOffset = sel_cale.get(Calendar.YEAR)-beg_cale.get(Calendar.YEAR);
						yearOffset = yearOffset%(int)intervalNum;
						if(yearOffset!=0){
							continue;
						}
						
					} else if ("month".equals(interval)) {
						if(sel_cale.get(Calendar.DAY_OF_MONTH)!=beg_cale.get(Calendar.DAY_OF_MONTH)){
							continue;  //天不同，本次取消
						}
						int yearOffset = sel_cale.get(Calendar.YEAR)-beg_cale.get(Calendar.YEAR);
						int monthOffset = sel_cale.get(Calendar.MONTH)-beg_cale.get(Calendar.MONTH);
						monthOffset = yearOffset*12 + monthOffset;
						monthOffset = monthOffset%(int)intervalNum;
						if(monthOffset!=0){
							continue;
						}
					} else {
						timeInterval = sel_cale.getTime().getTime() - beg_cale.getTime().getTime();  //毫秒差
						long intervalTime = intervalNum*DirectorConstants.intervalUintsOffsetMap.get(interval);
						long f = timeInterval%intervalTime;
						if(f>0F){   //根据是以前还是未来，取余后分为开始点的左侧和右侧，统一转为右侧
							f = f - intervalTime;
						}
						if( Math.abs(f) > 1000*3600*24 ){ //大于一天就不添加
							continue;
						}
						
					}
					AwokeBean bean = new AwokeBean();
					bean.setId( id );
					bean.setTitle( title );
					bean.setNextTime( nextTime );
					retList.add(bean);
				}
			}
			
		} catch (Exception e){
			log.error (e);
			e.printStackTrace();
		} finally {
			wzw.util.DbUtils.closeQuietly(conn, stmt, rs);
		}
		
		return retList;
	}
	
}
